﻿namespace Blaze.Editor.BehaviourTree
{
    using UnityEditor;
    using UnityEngine;

    public static class DesignerUtility
    {
        public static GUIStyle GraphBackgroundGUIStyle
        {
            get
            {
                if (graphBackgroundGUIStyle == null)
                    InitGraphBackgroundGUIStyle();
                return graphBackgroundGUIStyle;
            }
        }

        public static GUIStyle PropertyBoxGUIStyle
        {
            get
            {
                if (propertyBoxGUIStyle == null)
                {
                    InitPropertyBoxGUIStyle();
                }
                return propertyBoxGUIStyle;
            }
        }

        public static GUIStyle SelectionGUIStyle
        {
            get
            {
                if (selectionGUIStyle == null)
                    InitSelectionGUIStyle();
                return selectionGUIStyle;
            }
        }

        private static void InitGraphBackgroundGUIStyle()
        {
            Texture2D textured = new Texture2D(1, 1, TextureFormat.RGBA32, false, true);
            if (EditorGUIUtility.isProSkin)
            {
                textured.SetPixel(1, 1, new Color(0.1647f, 0.1647f, 0.1647f));
            }
            else
            {
                textured.SetPixel(1, 1, new Color(0.3647f, 0.3647f, 0.3647f));
            }
            textured.hideFlags = HideFlags.HideAndDontSave;
            textured.Apply();
            graphBackgroundGUIStyle = new GUIStyle(GUI.skin.box);
            graphBackgroundGUIStyle.normal.background = textured;
            graphBackgroundGUIStyle.active.background = textured;
            graphBackgroundGUIStyle.hover.background = textured;
            graphBackgroundGUIStyle.focused.background = textured;
            graphBackgroundGUIStyle.normal.textColor = Color.white;
            graphBackgroundGUIStyle.active.textColor = Color.white;
            graphBackgroundGUIStyle.hover.textColor = Color.white;
            graphBackgroundGUIStyle.focused.textColor = Color.white;
        }

        private static void InitPropertyBoxGUIStyle()
        {
            propertyBoxGUIStyle = new GUIStyle();
            propertyBoxGUIStyle.padding = new RectOffset(2, 2, 0, 0);
        }

        private static void InitSelectionGUIStyle()
        {
            Texture2D textured = new Texture2D(1, 1, TextureFormat.RGBA32, false, true);
            Color color = !EditorGUIUtility.isProSkin ? new Color(0.243f, 0.5686f, 0.839f, 0.5f) : new Color(0.188f, 0.4588f, 0.6862f, 0.5f);
            textured.SetPixel(1, 1, color);
            textured.hideFlags = HideFlags.HideAndDontSave;
            textured.Apply();
            selectionGUIStyle = new GUIStyle(GUI.skin.box);
            selectionGUIStyle.normal.background = textured;
            selectionGUIStyle.active.background = textured;
            selectionGUIStyle.hover.background = textured;
            selectionGUIStyle.focused.background = textured;
            selectionGUIStyle.normal.textColor = Color.white;
            selectionGUIStyle.active.textColor = Color.white;
            selectionGUIStyle.hover.textColor = Color.white;
            selectionGUIStyle.focused.textColor = Color.white;
        }

        private static GUIStyle graphBackgroundGUIStyle;
        private static GUIStyle selectionGUIStyle;
        private static GUIStyle propertyBoxGUIStyle;
    }
}